package hk.ust.comp3111.ustnutrition;

import hk.ust.comp3111.ustnutrition.data.DataAccessor;
import hk.ust.comp3111.ustnutrition.data.db.Food;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.ImageView.ScaleType;

@SuppressLint("NewApi")
public class FoodListCardView extends LinearLayout {
	public FoodListCardView(Context context)
	{
		super(context);
	}

	public FoodListCardView(Context context, AttributeSet attrs)
	{
		super(context, attrs);
	}

	public FoodListCardView(Context context, AttributeSet attrs, int defStyle)
	{
		super(context, attrs, defStyle);
	}

	public void setFood(Food food)
	{
		if (mTask != null)
		{
			mTask.cancel(true);
			mTask = null;
		}
		
		if(food == null)
			return;

		ViewHolder vh = (ViewHolder)getTag();
		vh.foodView.setText(food.getName());
		vh.restaurantView.setText(food.getRestaurantStr());
		vh.photoView.setImageDrawable(null);
		showFoodImage(food);

		vh.nutritionView.removeAllViews();
		if (food.getCarbohydrate() > 25)
		{
			vh.nutritionView.addView(createCarbohydrateIc());
		}
		if (food.getProtein() > 10)
		{
			vh.nutritionView.addView(createProteinIc());
		}
		if (food.getFat() > 15)
		{
			vh.nutritionView.addView(createFatIc());
		}
		if (food.getSodium() > 0.5)
		{
			vh.nutritionView.addView(createSodiumIc());
		}

		vh.ratingBar.setProgress((int)(food.getUserRating()));
	}

	@Override
	protected void onFinishInflate()
	{
		super.onFinishInflate();
		ViewHolder vh = new ViewHolder() {};
		vh.photoView = (ImageView)findViewById(R.id.photo);
		vh.foodView = (TextView)findViewById(R.id.food);
		vh.restaurantView = (TextView)findViewById(R.id.restaurant);
		vh.nutritionView = (LinearLayout)findViewById(R.id.nutrition);
		vh.ratingBar = (RatingBar)findViewById(R.id.score);

		setTag(vh);
	}

	private static class ViewHolder
	{
		public ImageView photoView;
		public TextView foodView;
		public TextView restaurantView;
		public LinearLayout nutritionView;
		public RatingBar ratingBar;
	}

	private ImageView createNutritionIc()
	{
		ImageView ic = new ImageView(getContext());
		int size = (int)MiscUtils.dpToPx(getContext(), 24);
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(size,
				size);
		params.rightMargin = (int)MiscUtils.dpToPx(getContext(), 8);
		ic.setLayoutParams(params);
		return ic;
	}

	private ImageView createCarbohydrateIc()
	{
		ImageView ic = createNutritionIc();
		ic.setImageDrawable(getContext().getResources().getDrawable(
				R.drawable.carbs));
		return ic;
	}

	private ImageView createProteinIc()
	{
		ImageView ic = createNutritionIc();
		ic.setImageDrawable(getContext().getResources().getDrawable(
				R.drawable.ic_protein));
		return ic;
	}

	private ImageView createFatIc()
	{
		ImageView ic = createNutritionIc();
		ic.setImageDrawable(getContext().getResources().getDrawable(
				R.drawable.fat));
		return ic;
	}

	private ImageView createSodiumIc()
	{
		ImageView ic = createNutritionIc();
		ic.setImageDrawable(getContext().getResources().getDrawable(
				R.drawable.sodium));
		return ic;
	}

	private void showFoodImage(Food food)
	{
		ViewHolder vh = (ViewHolder)getTag();
		final ImageView v = vh.photoView;
		mTask = new DownloadImageTask(v.getMeasuredWidth(), getMeasuredHeight())
				{
					@Override
					protected void onPostExecute(Bitmap result)
					{
						if (result != null)
						{
							v.setImageBitmap(result);
							v.setScaleType(ScaleType.CENTER_CROP);
						}
						else
						{
							v.setImageResource(android.R.drawable.ic_menu_gallery);
							v.setScaleType(ScaleType.CENTER);
						}
					}
				};
		mTask.execute(DataAccessor.Instance.getResource(food.getPhoto()));
	}

	private DownloadImageTask mTask;
}
